package com.chuangjiangx.member.score.ddd.domain.model;


import com.chuangjiangx.dddbase.Entity;
import com.chuangjiangx.dddbase.domain.shared.Timestamp;
import com.chuangjiangx.member.basic.ddd.domain.model.*;
import com.chuangjiangx.member.basic.ddd.domain.model.MerchantId;
import lombok.Getter;

import java.util.Date;

/**
 * 兑换记录
 *
 * @author LeeXyan
 * @Date 2018/1/26 15:48
 **/
@Getter
public class MbrScoreGiftRecord extends Entity<MbrScoreGiftRecordId> {

    /**
     * 会员id
     */
    private MemberId memberId;

    /**
     * 兑换规则id
     */
    private MbrScoreGiftRuleId mbrScoreGiftRuleId;

    /**
     * 商户id
     */
    private MerchantId merchantId;
    /**
     * 门店id
     */
    private Long storeId;

    /**
     * 服务员id
     */
    private Long storeUserId;

    /**
     * 商户用户id
     */
    private Long merchantUserId;


    /**
     * 取货码
     */
    private String claimCode;
    /**
     * 取货状态 0-未取货 1-已取货
     */
    private ClaimStatus claimStatus;

    /**
     * 取货时间
     */
    private Date claimTime;

    private MbrScoreStreamId mbrScoreStreamId;


    public MbrScoreGiftRecord(
            MbrScoreGiftRecordId id,
            MemberId memberId, MbrScoreGiftRuleId mbrScoreGiftRuleId,
            MerchantId merchantId, Long storeId, Long storeUserId,
            Long merchantUserId, Date createTime, Date updateTime,
            String claimCode, ClaimStatus claimStatus, Date claimTime,
            MbrScoreStreamId mbrScoreStreamId) {
        this.setId(id);
        this.memberId = memberId;
        this.mbrScoreGiftRuleId = mbrScoreGiftRuleId;
        this.merchantId = merchantId;
        this.storeId = storeId;
        this.storeUserId = storeUserId;
        this.merchantUserId = merchantUserId;
        this.setTimestamp(new Timestamp(createTime,updateTime));

        this.claimCode = claimCode;
        this.claimStatus = (claimStatus == null ? ClaimStatus.UNCLAIM : claimStatus);
        this.claimTime = claimTime;
        this.mbrScoreStreamId = mbrScoreStreamId;
    }

    /**
     * 领取货物
     */
    public void claimGoods(Long storeId,Long storeUserId,Long merchantUserId) {
        if(this.claimStatus == ClaimStatus.CLAIMED) {
            throw new IllegalStateException("该取货码已领取");
        }
        this.claimStatus = ClaimStatus.CLAIMED;
        this.claimTime = new Date();
        if(storeId != null){
            this.storeId = storeId;
            this.storeUserId = storeUserId;
        }

        if(merchantUserId != null){
            this.merchantUserId = merchantUserId;
        }
        this.update();
    }


}
